<?php

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Wire\AMQPTable;
use PhpAmqpLib\Message\AMQPMessage;

require __DIR__ . '/../../vendor/autoload.php';

$config = require(__DIR__ . '/../config.php');
$connection = new AMQPStreamConnection(
    $config['host'],
    $config['port'],
    $config['user'],
    $config['password'],
    $config['vhost']
);

$channel = $connection->channel();
$channel->confirm_select();

const CONFIRM_EXCHANE = 'confirm.exchange';
const CONFIRM_QUEUE = 'confirm.queue';
$channel->exchange_declare(CONFIRM_EXCHANE, 'direct', false, true, false);
$channel->queue_declare(CONFIRM_QUEUE,  false, true, false, false, false);
$channel->queue_bind(CONFIRM_QUEUE, CONFIRM_EXCHANE, 'rk_confirm');



$channel->basic_consume(CONFIRM_QUEUE, '', false, false, false, false, function(AMQPMessage $msg) {
    echo $msg->body . PHP_EOL;
    // if($msg->body == "index5") {
    //     $msg->nack();
    //     return ;
    // }

    // if($msg->body = 'index6') {
    //     $msg->reject();
    //     return ;
    // }

    return $msg->ack();
});

while ($channel->is_open()) {
    $channel->wait();
}

$channel->close();
$connection->close();